﻿function formatCurrencyRenderer(value, metaData, record, rowIndex, colIndex, store, view) {
    return formatMoney(value);
}

function formatMoney(v) {
    return Ext.util.Format.currency(v, Ext.util.Format.currencySign.concat(' '), Ext.util.Format.currencyPrecision);
}

function formatShortDate(v) {

    if (!v) return '';
    if (!(v instanceof Date)) v = new Date(Date.parse(v));
    return v.dateFormat('m/d/Y');
};

Ext.onReady(function () {
    Ext.USE_NATIVE_JSON = true;
});

function parseISO8601Date(iso8601) {
    var s = $.trim(iso8601);
    s = s.replace(/-/, "/").replace(/-/, "/");
    s = s.replace(/T/, " ").replace(/Z/, " UTC");
    s = s.replace(/([\+-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
    return new Date(s);
}

var _jsonParse = JSON.parse;
JSON.parse = function (jsonText) {

    return _jsonParse(jsonText,
        function (key, value) {
            var a;
            if (typeof value === 'string' && value.match(/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d{3}))?$/)) {
                a = parseISO8601Date(value);

                if (a && !isNaN(a)) {
                    return a;
                }
            }
            return value;
        });
};


Date.prototype.toJSON = function (key) {
    function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
    }

    return this.getFullYear() + '-' +
        f(this.getMonth() + 1) + '-' +
        f(this.getDate()) + 'T' +
        f(this.getHours()) + ':' +
        f(this.getMinutes()) + ':' +
        f(this.getSeconds()) + '.' +
        f(this.getMilliseconds());
};